package edu.aysf.service.impl;

import edu.aysf.dao.OrderDao;
import edu.aysf.entity.OrderEntity;
import edu.aysf.service.OrderService;
import edu.aysf.service.feign.AccountServiceFeign;
import edu.aysf.service.feign.GoodsServiceFeign;
import io.seata.spring.annotation.GlobalTransactional;
import lombok.extern.slf4j.Slf4j;
import org.apache.skywalking.apm.toolkit.trace.Trace;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;

/*
 * @Author 郑老师
 * @Version: 1.0
 * @Desc
 * @Date 2024-12-07 15:42
 * @Update
 */
@Service
@Slf4j
public class OrderServiceImpl implements OrderService{
    @Autowired
    private OrderDao orderDao;

    @Autowired
    private AccountServiceFeign accountServiceFeign;

    @Autowired
    private GoodsServiceFeign goodsServiceFeign;

    @Override
    public List<OrderEntity> selectOrderById(int user_id) {
        return orderDao.selectOrderById(user_id);
    }

    @Override
    public List<OrderEntity> selectOrder() {
        return orderDao.selectOrder();
    }

    @Override
    @Trace
    @GlobalTransactional(name="create-order", rollbackFor=Exception.class)
    public int createOrder(String id,int user_id, int goods_id, int count, Date create_time, String isdelete) {
        // 没有这个用户，也要抛出异常

        int i = orderDao.createOrder(id,user_id, goods_id, count, create_time,isdelete);

        // 减少库存
        reduceGoodsCount(goods_id, count);

        // 减少余额
        reduceAccountMoney(goods_id,user_id,count);

        return i;
    }

    // 减少库存
    @Trace
    @GlobalTransactional(name="create-order", rollbackFor=Exception.class)
    private void reduceGoodsCount(int goods_id,int count){
        HashMap map = new HashMap();
        map.put("id",goods_id);

        // feign查询指定商品库存
        int goods_count = goodsServiceFeign.selectCount(map);
        // 商品库存减去订单数量
        float update_count = goods_count - count;
        // 库存不够
        if(update_count<0) {
//          System.out.println("库存不足，无法进行扣减操作");
            throw new RuntimeException("库存不足，无法进行扣减操作");
        }
        // 没有这个商品，也要抛出异常

        int j = updateGoodsCount(goods_id, count);
    }

    // 减少账户余额
    @Trace
    @GlobalTransactional(name="create-order", rollbackFor=Exception.class)
    private void reduceAccountMoney(int goods_id,int user_id,int count){
        HashMap map = new HashMap();
        map.put("goods_id",goods_id);

        // 订单金额
        float orderMoney =  goodsServiceFeign.selectGoodsPrice(map) * count;

        map.put("user_id",user_id);
        // 用户的账户金额，feign调用
        float account_money = accountServiceFeign.selectMoney(map);
        // 账户余额减去订单金额
        float update_money = account_money - orderMoney;
        // 余额不够
        if(update_money<0) {
//          System.out.println("账户余额不足，无法进行扣减操作");
            throw new RuntimeException("账户余额不足，无法进行扣减操作");
        }
        // 没有这个账号用户，也要抛出异常

        int k = updateAccountMoney(user_id, orderMoney);
    }

    @Override
    public int updateGoodsCount(int goods_id, int count) {
        return orderDao.updateGoodsCount(goods_id, count);
    }

    @Override
    public int updateAccountMoney(int user_id, float money) {
        return orderDao.updateAccountMoney(user_id, money);
    }

    @Override
    public int updateOrderCount(String id, String isdelete) {
        return orderDao.updateOrderCount(id, isdelete);
    }
}
